package main

import (
	"context"
	"fmt"
	"strings"
	"time"
)

var TraceId = "traceId"

func main() {
	newUUID := "1-2-3-4"
	randId := strings.ReplaceAll(newUUID, "-", "")
	//设置上下文
	ctx := context.WithValue(context.Background(), TraceId, randId)
	//总共三层深度的上下文树
	//如果当前层级没有key时，则会想当前上下文的父级上下文寻找key值
	userCtx := context.WithValue(ctx, "user", "user")
	go printUserInfo(userCtx)
	time.Sleep(time.Second)
}

func printUserInfo(ctx context.Context) {
	//如果找不到，则会返回nil空值
	fmt.Println(ctx.Value(TraceId))
}
